﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SalesUserService
{

    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    public string saveSalesUser(string location, string name, string emailId, long mobileNo)
	{
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pSalesuserInsert";
        cmd.Parameters.Add(new SqlParameter("@userLocation", SqlDbType.VarChar)).Value = location;
        cmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar)).Value = name;
        cmd.Parameters.Add(new SqlParameter("@userEmail", SqlDbType.VarChar)).Value = emailId;
        cmd.Parameters.Add(new SqlParameter("@userMobile", SqlDbType.BigInt)).Value = mobileNo;       
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();

        return "{Message: '" + ds.Tables[0].Rows[0]["Message"] + "', Confirmation: '" + ds.Tables[0].Rows[0]["Confirmation"] + "'}";
	
	}


    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]

    public string saveCustomerInformation(string username, string password, string cpassword, string firstname, string lastname, string mobile, string email, string address1, string address2, string state, string country)
    {
        try
        {

            DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "MAS_pCustomerRegistration";
            cmd.Parameters.Add(new SqlParameter("@CustUsername", SqlDbType.VarChar)).Value = username;
            cmd.Parameters.Add(new SqlParameter("@CustPassword", SqlDbType.VarChar)).Value = password;
            cmd.Parameters.Add(new SqlParameter("@CustConfirmPassword", SqlDbType.VarChar)).Value = cpassword;
            cmd.Parameters.Add(new SqlParameter("@CustFirstname", SqlDbType.VarChar)).Value = firstname;
            cmd.Parameters.Add(new SqlParameter("@CustLastname", SqlDbType.VarChar)).Value = lastname;
            cmd.Parameters.Add(new SqlParameter("@CustMobile", SqlDbType.VarChar)).Value = mobile;
            cmd.Parameters.Add(new SqlParameter("@CustEmail", SqlDbType.VarChar)).Value = email;
            cmd.Parameters.Add(new SqlParameter("@CustAddress1", SqlDbType.VarChar)).Value = address1;
            cmd.Parameters.Add(new SqlParameter("@CustAddress2", SqlDbType.VarChar)).Value = address2;
            cmd.Parameters.Add(new SqlParameter("@CustState", SqlDbType.VarChar)).Value = state;
            cmd.Parameters.Add(new SqlParameter("@CustCountry", SqlDbType.VarChar)).Value = country;


            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            con.Open();
            sda.Fill(ds);
            con.Close();

            return "{Message: '" + ds.Tables[0].Rows[0]["Message"] + "', Confirmation: '" + ds.Tables[0].Rows[0]["Confirmation"] + "'}";
        }
        catch (Exception)
        {
            return "{Message: 'Failed' ,Status :'2' }";
        }
    }




    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string fetchUserDetails(int UserID)
    {
        
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pFetchSalesUser";
        cmd.Parameters.Add("@p_UserId", SqlDbType.Int).Value = UserID;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();
        return "{salesUserLocation:'" + ds.Tables[0].Rows[0]["salesUserLocation"] + "',salesUserName: '" + ds.Tables[0].Rows[0]["salesUserName"] +
                "',salesUserEmail:'" + ds.Tables[0].Rows[0]["salesUserEmail"] + "',salesUserMobile:'" + ds.Tables[0].Rows[0]["salesUserMobile"] +
                "'}";
    }




    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string modifySalesUser(int userId, string UserLocation, string UserName,string UserEmail, string UserMobile)
    {
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pSalesuserUpdate";
        cmd.Parameters.Add(new SqlParameter("@userId", SqlDbType.Int)).Value = userId;
        cmd.Parameters.Add(new SqlParameter("@userLocation", SqlDbType.VarChar)).Value = UserLocation;
        cmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar)).Value = UserName;
        cmd.Parameters.Add(new SqlParameter("@userEmail", SqlDbType.VarChar)).Value = UserEmail;
        cmd.Parameters.Add(new SqlParameter("@userMobile", SqlDbType.VarChar)).Value = UserMobile;
        
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();

        return "{Message: '" + ds.Tables[0].Rows[0]["Message"] + "'}";

    }


    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string fetchUser(int UserID)
    {

        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pFetchSalesUser";
        cmd.Parameters.Add("@p_UserId", SqlDbType.Int).Value = UserID;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();
        return "{salesUserLocation:'" + ds.Tables[0].Rows[0]["salesUserLocation"] + "',salesUserName: '" + ds.Tables[0].Rows[0]["salesUserName"] +
                "',salesUserEmail:'" + ds.Tables[0].Rows[0]["salesUserEmail"] + "',salesUserMobile:'" + ds.Tables[0].Rows[0]["salesUserMobile"] +
                "'}";
    }

    
    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string removeSalesUser(int UserId)
    {
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pSalesuserDelete";
        cmd.Parameters.Add(new SqlParameter("@UserID", SqlDbType.VarChar)).Value = UserId;
        
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();

        return "{Message: '" + ds.Tables[0].Rows[0]["Message"] + "'}";

    }
  


}
